package com.bc.bczbweb.controller;

import com.bc.bcapi.dao.T_User_seller_bonusMapper;
import com.bc.bcapi.service.*;
import com.bc.bccore.webconfig.WebConfig;
import com.bc.bccore.zbconfig.ZbConfig;
import com.bc.bcmodel.domain.*;
import com.bc.bcmodel.req.MngProjectListReq;
import com.bc.bcmodel.resp.MngProjectListResp;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigInteger;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMethod;
import com.bc.bccore.util.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("t_project")
public class T_ProjectController extends BaseController {
    @Autowired
    T_ProjectService t_ProjectService;
    @Autowired
    T_Project_contactService t_project_contactService;
    @Autowired
    T_User_seller_bonusMapper t_user_seller_bonusMapper;
    @Autowired
    T_File_upload_logService t_file_upload_logService;
    @Autowired
    T_Erc_contractService t_erc_contractService;
    @Autowired
    T_Project_exchangeService t_project_exchangeService;
    @Autowired
    T_Project_teamService t_project_teamService;
    @Autowired
    T_Project_enService t_project_enService;

    @RequestMapping("list")
    public ModelAndView list() {
        ModelAndView view = super.baseView("列表", "t_project/list");
        return view;
    }

    @RequestMapping(value = "listdo", method = RequestMethod.POST)
    public ModelAndView listdo(MngProjectListReq req) {
        ModelAndView view = super.baseViewAjax("列表", "t_project/listdo");
        PageInfo<MngProjectListResp> pageInfo = t_ProjectService.getPage(req);
        view.addObject("data", pageInfo.getList());
        view.addObject("page", PageHelper.GetAjaxHtml(pageInfo, "javascript:loadpage(##);"));
        return view;
    }

    @RequestMapping("add")
    public ModelAndView add(String pid) throws Exception {
        if (StringUtils.isEmpty(pid)) {
            throw new Exception("当前pid不能为空");
        }
        String title = "查看并审核";
        ModelAndView view = super.baseView(title, "t_project/add");
        T_ProjectModel t_projectModel = new T_ProjectModel();
        t_projectModel.setPid(pid);
        T_ProjectModel data = t_ProjectService.findByNamedParam(t_projectModel);
        T_Project_contactModel t_project_contactModel = new T_Project_contactModel();
        t_project_contactModel.setPid(pid);
        List<T_Project_contactModel> list_contact =
                t_project_contactService.getAllList(t_project_contactModel);

        T_User_seller_bonusModel t_user_seller_bonusModel = new T_User_seller_bonusModel();
        t_user_seller_bonusModel.setUser_id(t_projectModel.getUser_id());
        List<T_User_seller_bonusModel> list_bonus =
                t_user_seller_bonusMapper.findByNamedParamList(t_user_seller_bonusModel);

        /**
         * 查询digit
         * */
        int digit = 0;
        T_Erc_contractModel t_erc_contractModel = new T_Erc_contractModel();
        t_erc_contractModel.setPid(pid);
        T_Erc_contractModel t_erc_contractModelResult = t_erc_contractService.findByNamedParam(t_erc_contractModel);
        if (t_erc_contractModelResult != null) {
            digit = t_erc_contractModelResult.getDigit();
        }

        T_Project_exchangeModel t_project_exchangeModel =
                new T_Project_exchangeModel();
        t_project_exchangeModel.setPid(pid);
        List<T_Project_exchangeModel> listExchange =
                t_project_exchangeService.getAllList(t_project_exchangeModel);

        T_Project_teamModel t_project_teamModel = new T_Project_teamModel();
        t_project_teamModel.setPid(pid);
        List<T_Project_teamModel> listTeam = t_project_teamService.getAllList(t_project_teamModel);

        T_Project_enModel t_project_enModel = new T_Project_enModel();
        t_project_enModel.setPid(pid);
        T_Project_enModel projectEn = t_project_enService.findByNamedParam(t_project_enModel);

        view.addObject("listExchange", listExchange);
        view.addObject("digit", digit);
        view.addObject("list_contact", list_contact);
        view.addObject("list_bonus", list_bonus);
        view.addObject("data", data);
        view.addObject("title", title);
        view.addObject("listTeam", listTeam);
        /*英文*/
        view.addObject("projectEn", projectEn);
        view.addObject("whitePaperEn", projectEn == null ? "" : ZbConfig.getImgUrl() + projectEn.getWhitepaper_en());

        return view;
    }
}